package com.wwy;

import com.wwy.domain.RssiEntity;
import com.wwy.domain.TypeEntity;
import com.wwy.service.LableSaveService;
import com.wwy.service.RssiService;
import org.springframework.context.ApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author: wwy
 * @Date: 2020/8/14 15:18
 */
public class RssiDialog extends JDialog implements ActionListener {
    public static ApplicationContext applicationContext;
    public static RssiEntity rssiEntity = new RssiEntity();
    JLabel lableTitle;
    public JTextField lableText;
    public JTextField lableText1;
    JButton quitButton;
    JButton saveButton;
    public RssiDialog(JFrame F,String s){
        super(F,s,true);//模态
        this.setBounds(200, 200, 700, 540);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,1));
        panel.setSize(700,540);

        JPanel topPanel = new JPanel();
        topPanel.setVisible(true);
        topPanel.setLayout(new FlowLayout());
        panel.add(topPanel);
        lableTitle = new JLabel("阀值范围");
        JLabel jLabel = new JLabel("-");
        lableText = new JTextField("",10);
        lableText1 = new JTextField("",10);
        lableText.setText(rssiEntity.getStart()+"");
        lableText1.setText(rssiEntity.getEnd()+"");
        lableText.setEditable(true);
        lableText1.setEditable(true);
        topPanel.add(lableTitle);
        topPanel.add(lableText);
        topPanel.add(jLabel);
        topPanel.add(lableText1);
        JPanel top1Panel = new JPanel();
        top1Panel.setVisible(true);
        top1Panel.setLayout(new FlowLayout());
        JPanel top2Panel = new JPanel();
        top2Panel.setVisible(true);
        top2Panel.setLayout(new FlowLayout());
        quitButton = new JButton("取消");
        quitButton.setActionCommand("quit");
        quitButton.addActionListener(this);
        saveButton = new JButton("确定");
        saveButton.setActionCommand("save");
        saveButton.addActionListener(this);
        top2Panel.add(quitButton);
        top2Panel.add(saveButton);
        panel.add(top2Panel);
        panel.setVisible(true);
        setModal(false);
        this.setContentPane(panel);
        this.pack();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()){
            case "quit":
                System.out.println("执行退出");
                dispose();
                break;
            case "save":
                System.out.println("执行保存");
                //调用新增代码
                RssiService rssiService = applicationContext.getBean(RssiService.class);

                RssiEntity rssiEntity1 = new RssiEntity();
                rssiEntity1.setStart(Integer.parseInt(lableText.getText()));
                rssiEntity1.setEnd(Integer.parseInt(lableText1.getText()));
                rssiEntity = rssiEntity1;
                rssiService.save(rssiEntity);
                dispose();
                break;
        }
    }
}
